package com.awesome.javademo.threads.lock.waitnotift.bean;


import com.awesome.common.util.log.ShowLogUtil;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2021/8/2 7:57 下午
 * Description:WaitNotify锁机制
 */
public class WaitNotify {
    private Object obj = new Object();
    private Thread thread1;
    private Thread thread2;

    public void startThread() {
        thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info(Thread.currentThread().getName() + " wait start");
                synchronized (obj) {
                    ShowLogUtil.info(Thread.currentThread().getName() + " wait synchronized start");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ShowLogUtil.info(Thread.currentThread().getName() + " wait synchronized finished");
                }
                ShowLogUtil.info(Thread.currentThread().getName() + " wait finished");
            }
        });
        thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info(Thread.currentThread().getName() + " notify start");
                synchronized (obj) {
                    ShowLogUtil.info(Thread.currentThread().getName() + " notify synchronized start");
                    obj.notify();
                    ShowLogUtil.info(Thread.currentThread().getName() + " notify synchronized finished");
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info(Thread.currentThread().getName() + " notify finished");
            }
        });
        startThread1();
        startThread2();
    }

    public void startThread1() {
        thread1.start();
    }

    public void startThread2() {
        thread2.start();
    }

}